import md5 from 'js-md5';
let process = import.meta.env;
console.log("process.env", process);
const api_sign_secret = process.VITE_APP_API_SIGN_SECRET;
const tagInfo = "/utils/net/Sign.js";
export default function (data) {
  if (!data) {
    data = {};
  }
  const timestamp = parseInt((new Date()).getTime() / 1000);


  // console.log("timestamp,",timestamp,data);
  data._timestamp = timestamp;
  const sortData = {};
  Object.keys(data).sort().forEach((key) => {
    sortData[key] = parseString(data[key]);
  });
  console.log(tagInfo, JSON.stringify(sortData) + md5(api_sign_secret));
  sortData._sign = md5(JSON.stringify(sortData) + md5(api_sign_secret));
  return sortData;
};

function parseString(obj) {
  if (!obj && typeof obj !== 'undefined' && obj != 0) {
    return '';
  }
  if (typeof obj === 'undefined') {
    return '';
  }
  if (typeof obj === 'number' && isNaN(obj)) {
    return '0';
  }
  return obj.toString();
}